package com.hk.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hk.reggie.common.BaseContext;
import com.hk.reggie.common.R;
import com.hk.reggie.entity.ShoppingCart;
import com.hk.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody  ShoppingCart shoppingCart){
        //设置用户id，确定当前是哪个用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询当前菜品是否在购物车中
        Long dishId = shoppingCart.getDishId();

        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", currentId);
        if(dishId != null){
            //添加购物车进的是菜品
            queryWrapper.eq("dish_id", dishId);
        }else{
            //添加购物车进的是套餐
            queryWrapper.eq("setmeal_id", shoppingCart.getSetmealId());
        }

        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);
        if(shoppingCartOne != null){
            //如果已存在，就在原来数量基础上加一
            int curCount = shoppingCartOne.getNumber();
            shoppingCartOne.setNumber(curCount + 1);
            shoppingCartService.updateById(shoppingCartOne);
        }else{
            //如果不存在，就新增一个
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartOne = shoppingCart;
        }

        return R.success(shoppingCartOne);
    }


    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.orderByAsc("create_time");

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        shoppingCartService.clean();
        return R.success("清空购物车成功");
    }


    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody  ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();

        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", currentId);
        if(shoppingCart.getDishId() != null){
            queryWrapper.eq("dish_id", shoppingCart.getDishId());
        }else{
            queryWrapper.eq("setmeal_id", shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(queryWrapper);
        int curNum = shoppingCartOne.getNumber();
        shoppingCartOne.setNumber(curNum - 1);
        if(shoppingCartOne.getNumber().equals(0)){
            shoppingCartService.removeById(shoppingCartOne.getId());
        }else{
            shoppingCartService.updateById(shoppingCartOne);
        }
        return R.success(shoppingCartOne);
    }
}
